

<!DOCTYPE html>
<html lang="zh-CN" color-mode=light>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Jvm - 唱跳rap</title>
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="google" content="notranslate" />
  
  <meta name="description" content="JVMJvm组成和运行流程
Java Virtual ...">
  <meta name="author" content="东子">
  <link rel="icon" href="/images/icons/icon-29.png" type="image/png" sizes="16x16">
  <link rel="icon" href="/images/icons/icon-29@2x.png" type="image/png" sizes="32x32">
  <link rel="apple-touch-icon" href="/images/icons/icon-1024.png" sizes="180x180">
  <meta rel="mask-icon" href="/images/icons/stun-logo.svg" color="#333333">
  
    <meta rel="msapplication-TileImage" content="/images/icons/icon-1024.png">
    <meta rel="msapplication-TileColor" content="#000000">
  

  
<link rel="stylesheet" href="/css/style.css">


  
    
<link rel="stylesheet" href="https://at.alicdn.com/t/font_1445822_p6ry5n7lrr.css">

  

  
    
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  

  
    
      
        
        
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/xcode.min.css" name="highlight-style" mode="light">

      
        
        
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/solarized-dark.min.css" name="highlight-style" mode="dark">

      
  

  <script>
    var CONFIG = window.CONFIG || {};
    var ZHAOO = window.ZHAOO || {};
    CONFIG = {
      isHome: false,
      fancybox: true,
      pjax: false,
      loading: {
        gif: '',
        lottie: ''
      },
      lazyload: {
        enable: true,
        only_post: 'false',
        loading: {
          gif: '',
          lottie: ''
        }
      },
      donate: {
        enable: false,
        alipay: 'https://pic.izhaoo.com/alipay.jpg',
        wechat: 'https://pic.izhaoo.com/wechat.jpg'
      },
      galleries: {
        enable: false
      },
      fab: {
        enable: true,
        always_show: false
      },
      carrier: {
        enable: true
      },
      daovoice: {
        enable: false
      },
      preview: {
        background: {
          default: '',
          api: 'https://api.paugram.com/bing/'
        },
        motto: {
          default: '我在开了灯的床头下，想问问自己的心啊。',
          typing: true,
          api: 'https://v2.jinrishici.com/one.json',
          data_contents: '["data","content"]'
        },
      },
      qrcode: {
        enable: true,
        type: 'url',
        image: 'https://pic.izhaoo.com/weapp-code.jpg',
      },
      toc: {
        enable: true
      },
      scrollbar: {
        type: 'default'
      },
      notification: {
        enable: false,
        delay: 4500,
        list: '',
        page_white_list: '',
        page_black_list: ''
      },
      search: {
        enable: false,
        path: ''
      }
    }
  </script>

  

  

<meta name="generator" content="Hexo 6.3.0"></head>

<body class="lock-screen">
  <div class="loading" id="loading"></div>
  
    


  <nav class="navbar">
    <div class="left">
      
        <i class="iconfont iconhome j-navbar-back-home"></i>
      
      
        <i class="iconfont iconqrcode j-navbar-qrcode"></i>
      
      
        <i class="iconfont iconmoono" id="color-toggle" color-toggle="light"></i>
      
      
    </div>
    <div class="center">Jvm</div>
    <div class="right">
      <i class="iconfont iconmenu j-navbar-menu"></i>
    </div>
    
      <div id="qrcode-navbar"></div>
    
  </nav>

  
  

<nav class="menu">
  <div class="menu-container">
    <div class="menu-close">
      <i class="iconfont iconbaseline-close-px"></i>
    </div>
    <ul class="menu-content"><li class="menu-item">
        <a href="/ " class="underline "> 首页</a>
      </li><li class="menu-item">
        <a href="/archives/ " class="underline "> 归档</a>
      </li><li class="menu-item">
        <a href="/categories/ " class="underline "> 分类</a>
      </li><li class="menu-item">
        <a href="/about/ " class="underline "> 关于</a>
      </li></ul>
    
      <div class="menu-copyright"><p>Powered by <a target="_blank" href="https://gitee.com/comebackearly">东子</a>  | 爷-青结~~</p></div>
    
  </div>
</nav>
  <main id="main">
  <div class="article-wrap">
    <div class="row container">
      <div class="col-xl-3"></div>
      <div class="col-xl-6"><article class="article">
  <div class="wrap">
    <section class="head">
  <img   class="lazyload" data-original="https://cdn.educba.com/academy/wp-content/uploads/2019/04/What-is-JVM.jpg" src=""  draggable="false">
  <div class="head-mask">
    <h1 class="head-title">Jvm</h1>
    <div class="head-info">
      <span class="post-info-item"><i class="iconfont iconcalendar"></i>八月 21, 2023</span>
      
      <span class="post-info-item"><i class="iconfont iconfont-size"></i>1371</span>
    </div>
  </div>
</section>
    <section class="main">
      <section class="content">
        
        <h2 id="JVM"><a href="#JVM" class="headerlink" title="JVM"></a>JVM</h2><h2 id="Jvm组成和运行流程"><a href="#Jvm组成和运行流程" class="headerlink" title="Jvm组成和运行流程"></a>Jvm组成和运行流程</h2><blockquote>
<p>Java Virtual Machine Java程序的运行环境（java二进制字节码的运行环境）一次编写，到处运行 ,自动内存管理，垃圾回收机制</p>
</blockquote>
<ul>
<li><p>组成</p>
<p><img   class="lazyload" data-original="/image-20230822111245793.png" src=""  alt="image-20230822111245793"></p>
</li>
<li><p>执行流程</p>
<p>（1）<strong>类加载器</strong>（ClassLoader）<strong>把Java代码转换为字节码</strong> </p>
<p>（2）<strong>运行时数据区</strong>（Runtime Data Area）<strong>把字节码加载到内存中</strong>，而字节码文 件只是JVM的一套指令集规范，并不能直接交给底层系统去执行，而是有执行引 擎运行</p>
<p>（3）<strong>执行引擎</strong>（Execution Engine）<strong>将字节码翻译为底层系统指令</strong>，再交由CPU 执行去执行，此时需要调用其他语言的本地库接口（Native Method Library）来 实现整个程序的功能</p>
</li>
</ul>
<h2 id="什么是程序计数器"><a href="#什么是程序计数器" class="headerlink" title="什么是程序计数器"></a>什么是程序计数器</h2><blockquote>
<p>程序计数器：线程私有的，内部保存的字节码的行号。用于<strong>记录正在执行的字节码指令的地址</strong>。</p>
</blockquote>
<h2 id="Java堆"><a href="#Java堆" class="headerlink" title="Java堆"></a>Java堆</h2><blockquote>
<p>线程共享的区域：主要用来<strong>保存对象实例，数组</strong>等，当堆中没有内存空间可分配给实例，也无法再扩展时，则抛出 <strong>OutOfMemoryError</strong>异常。</p>
<p>组成：<strong>年轻代+老年代</strong></p>
</blockquote>
<p><img   class="lazyload" data-original="/image-20230822112756849.png" src=""  alt="image-20230822112756849"></p>
<ul>
<li><p><strong>年轻代</strong>被划分为三部分，Eden区和两个大小严格相同的Survivor区 </p>
</li>
<li><p><strong>老年代</strong>主要保存生命周期长的对象，一般是一些老的对象</p>
<ul>
<li>1.7中有有一个永久代，存储的是<strong>类信息、静态变量、常量、编译后的代码</strong> </li>
<li>1.8移除了永久代，把数据存储到了本地内存的<strong>元空间</strong>中，防止内存溢出</li>
</ul>
</li>
</ul>
<h2 id="什么是虚拟机栈"><a href="#什么是虚拟机栈" class="headerlink" title="什么是虚拟机栈"></a>什么是虚拟机栈</h2><blockquote>
<p><strong>每个线程运行时所需要的内存</strong>，称为虚拟机栈</p>
<p><strong>每个栈由多个栈帧（frame）组成</strong>，对应着每次方法调用时所占用的内存,每个线程只能有<strong>一个活动栈帧</strong>，对应着当前正在执行的那个方法</p>
</blockquote>
<ul>
<li><p>栈内存分配越大越好吗</p>
<p>默认的栈内存通常为1024k，<strong>栈帧过大会导致线程数变少</strong></p>
</li>
<li><p>方法内的局部变量<strong>是否线程安全</strong></p>
</li>
</ul>
<p>  如果是局部变量<strong>引用了对象</strong>，并逃离方法的作用范围，需要考虑线程安全,其余情况是安全的</p>
<ul>
<li><p>什么情况下会导致栈内存溢出</p>
<p>栈帧过多导致栈内存溢出，典型问题：<strong>递归调用</strong> </p>
<p><strong>栈帧过大</strong>导致栈内存溢出</p>
</li>
</ul>
<h2 id="方法区-元空间"><a href="#方法区-元空间" class="headerlink" title="方法区(元空间)"></a>方法区(元空间)</h2><blockquote>
<p>方法区(Method Area)是各个线程<strong>共享的内存</strong>区域,主要存储<strong>类的信息、运行时常量池</strong></p>
<p> 如果方法区域中的内存无法满足分配请求，则会抛出OutOfMemoryError: Metaspace</p>
</blockquote>
<ul>
<li><p>常量池</p>
<blockquote>
<p>可以看作是<strong>一张表</strong>，虚拟机指令根据这张常量表找到要执行的类名、方法名、参数类型、字面量等信息</p>
</blockquote>
</li>
<li><p>运行时常量池</p>
<blockquote>
<p>常量池是 <em>.class 文件中的，<strong>当该类被加载，它的常量池信息就会放入运行时常量池</strong>，*<em>并把里面的符号地址变为真实地址</em></em></p>
</blockquote>
</li>
</ul>
<h2 id="直接内存"><a href="#直接内存" class="headerlink" title="直接内存"></a>直接内存</h2><blockquote>
<p>并不属于JVM中的内存结构，<strong>不由JVM进行管理</strong>。是虚拟机的系统内存，常见于 <strong>NIO</strong> 操作时，用于数据缓冲 区，它<strong>分配回收成本较高</strong>，<strong>但读写性能高</strong></p>
</blockquote>
<p>NIO读写文件只用一次缓存操作</p>
<h2 id="堆和栈的区别是什么"><a href="#堆和栈的区别是什么" class="headerlink" title="堆和栈的区别是什么"></a>堆和栈的区别是什么</h2><p>栈内存一般会用来<strong>存储局部变量和方法调用</strong>，但堆内存是用来存储Java对象和 数组的的。堆会<strong>GC垃圾回收</strong>，而栈不会。</p>
<p>栈内存是<strong>线程私有</strong>的，而堆内存是线程共有的。</p>
<p>两者<strong>异常</strong>错误不同，但如果栈内存或者堆内存不足都会抛出异常。</p>
<p> 栈空间不足：java.lang.StackOverFlowError。</p>
<p> 堆空间不足：java.lang.OutOfMemoryError。</p>

      </section>
      <section class="extra">
        
          <ul class="copyright">
  
    <li><strong>本文作者：</strong>东子</li>
    <li><strong>本文链接：</strong><a href="https://comebackearly.gitee.io/2023/08/21/Jvm/index.html" title="https:&#x2F;&#x2F;comebackearly.gitee.io&#x2F;2023&#x2F;08&#x2F;21&#x2F;Jvm&#x2F;index.html">https:&#x2F;&#x2F;comebackearly.gitee.io&#x2F;2023&#x2F;08&#x2F;21&#x2F;Jvm&#x2F;index.html</a></li>
    <li><strong>版权声明：</strong>本博客所有文章均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" title="BY-NC-SA" target="_blank" rel="noopener">BY-NC-SA</a> 许可协议，转载请注明出处！</li>
  
</ul>
        
        
        
        
  <nav class="nav">
    <a href="/2023/09/23/%E9%9D%A2%E8%AF%95%E5%8F%8D%E9%97%AE/"><i class="iconfont iconleft"></i>面试反问</a>
    <a href="/2023/08/16/springMybatis/">springMybatis<i class="iconfont iconright"></i></a>
  </nav>

      </section>
      
        <section class="comments">
  
    <div class="btn" id="comments-btn">查看评论</div>
  
  
<div id="valine"></div>
<script defer src="//unpkg.com/valine/dist/Valine.min.js"></script>
<script>
  window.onload = function () {
    var loadValine = function () {
      new Valine({
        el: '#valine',
        app_id: "833C0IwLe9o8QE1pN25Mv59F-gzGzoHsz",
        app_key: "YLQZdn7egrZOmJnwr6YFEw0y",
        placeholder: "雁过留痕",
        avatar: "mp",
        pageSize: "10",
        lang: "zh-CN",
      });
    }
    if ( true ) {
      $("#comments-btn").on("click", function () {
        $(this).hide();
        loadValine();
      });
    } else {
      loadValine();
    }
  };
</script>

</section>
      
    </section>
  </div>
</article></div>
      <div class="col-xl-3">
        
          
  <aside class="toc-wrap">
    <h3 class="toc-title">文章目录：</h3>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JVM"><span class="toc-text">JVM</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Jvm%E7%BB%84%E6%88%90%E5%92%8C%E8%BF%90%E8%A1%8C%E6%B5%81%E7%A8%8B"><span class="toc-text">Jvm组成和运行流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E7%A8%8B%E5%BA%8F%E8%AE%A1%E6%95%B0%E5%99%A8"><span class="toc-text">什么是程序计数器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java%E5%A0%86"><span class="toc-text">Java堆</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%A0%88"><span class="toc-text">什么是虚拟机栈</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E5%8C%BA-%E5%85%83%E7%A9%BA%E9%97%B4"><span class="toc-text">方法区(元空间)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%B4%E6%8E%A5%E5%86%85%E5%AD%98"><span class="toc-text">直接内存</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A0%86%E5%92%8C%E6%A0%88%E7%9A%84%E5%8C%BA%E5%88%AB%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-text">堆和栈的区别是什么</span></a></li></ol>
  </aside>

        
      </div>
    </div>
  </div>
</main>
  

<footer class="footer">
  <div class="footer-social"><a 
        href="tencent://message/?Menu=yes&uin=2911890696 "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#12B7F5'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconQQ "></i>
      </a><a 
        href="javascript:; "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#09BB07'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconwechat-fill "></i>
      </a><a 
        href="https://github.com/nuban "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#9f7be1'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  icongithub-fill "></i>
      </a></div>
  
    <div class="footer-copyright"><p>Powered by <a target="_blank" href="https://gitee.com/comebackearly">东子</a>  | 爷-青结~~</p></div>
  
  <link rel="stylesheet" href="/dist/APlayer.min.css">
  <div id="aplayer"></div>
  <script type="text/javascript" src="/dist/APlayer.min.js"></script>
  <script type="text/javascript" src="/dist/music.js"></script>
  
</footer>
  
      <div class="fab fab-plus">
    <i class="iconfont iconplus"></i>
  </div>
  
  
  
  <div class="fab fab-up">
    <i class="iconfont iconcaret-up"></i>
  </div>
  
  
  
    
<script src="/js/color-mode.js"></script>

  
  
</body>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>





  
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script>




  
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>






  
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>




<script src="/js/utils.js"></script>
<script src="/js/script.js"></script>







  <script>
    (function () {
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      } else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>



  <script defer src='https://hm.baidu.com/hm.js?dda12e485d1b1d3113db8147f482111c'></script>











</html>